package cn.tedu.mall.producer.controller;


import cn.tedu.mall.producer.pojo.dto.AttributeAddNewDTO;
import cn.tedu.mall.producer.pojo.vo.AttributeListItemVO;
import cn.tedu.mall.producer.service.IAttributeService;
import cn.tedu.mall.producer.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 属性控制器
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Slf4j
@Api(tags = "4. 属性管理模块")
@RequestMapping("/attributes")
@RestController
public class AttributeController {

    @Autowired
    private IAttributeService attributeService;

    public AttributeController() {
        log.debug("创建控制器对象：AttributeController");
    }

    // http://localhost:9080/attributes/add-new
    @ApiOperation("添加属性")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult addNew(@RequestBody AttributeAddNewDTO attributeAddNewDTO) {
        log.debug("接收到的请求参数：{}", attributeAddNewDTO);
        attributeService.addNew(attributeAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/attributes/12/delete
    @ApiOperation("删除属性")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult delete(@PathVariable Long id) {
        attributeService.deleteById(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/attributes
    @ApiOperation("查询属性列表")
    @ApiOperationSupport(order = 400)
    @GetMapping("")
    public JsonResult list() {
        log.debug("接收到查询属性列表的请求");
        List<AttributeListItemVO> attributes = attributeService.list();
        return JsonResult.ok(attributes);
    }

}
